//# sourceURL=http://uMapApp/Module/PQI/PQIBlock
define([
    "mhjs/mhjs"
    , "../../UI/Panel/PanelBlock/PanelBlock"
    , "./LevelChart"
], function (
    mhjs,
    PanelBlock,
    LevelChart
) {

    function PQIBlock(option) {

        var _this = this;
        _this.isOpened = false;

        _this.ui = {
            params: new mhjs.Div({"class": "param"})
        };

        PanelBlock.call(_this, option);
        _this.addClass("PQIBlock propertiesBlock");

    }

    var _class = PQIBlock;
    mhjs.Inherit(_class, PanelBlock);


    Object.defineProperties(_class.prototype, {
        "properties"       : {
            set: function (properties) {
                var _this = this;
                _this._properties = properties;

                _this.ui.wrap.empty();

                _this.title = "路面状况信息";

                _this.ui.wrap
                    .append(_this.renderBasic())
                    .append(_this.renderRelative())
                    .append(_this.renderParam())
                    .append(_this.renderYearCompare())
                ;

                properties.on("update", function () {
                    _this.levelChart.param = {
                        label: _this.properties.currentParam,
                        value: _this.properties.param[_this.properties.currentParam],
                        level: _this.properties.currentLevel
                    };
                });


            },
            get: function () {
                return this._properties;
            }
        },
        "renderBasic"      : {
            value: function () {
                var _this = this;

                return _this.renderTree("基础信息", {
                    "线路编号": "G50",
                    "线路名称": "沪苏浙高速",
                    "车道数" : _this.properties["车道"],
                    "通车时间": "2008年",
                    "管养单位": "黎里养护工区",
                    "桩号"  : _this.properties["开始桩号"] + " - " + _this.properties["结束桩号"]
                });

            }
        },
        "renderRelative"   : {
            value: function () {
                var _this = this;

                var result = _this.renderTree("关联信息", {
                    "路龄"     : "15年",
                    "软基路段"   : "",
                    "交通量"    : "",
                    "交通等级"   : "中",
                    "路面结构与材料": _this.properties.app.modules.road.roadMaterialsModule.getStakeNumberMaterials(_this.properties["开始桩号"])
                });

                result.label.on("click", function () {
                    result.toggleClass("close");
                });

                result
                    .addClass("more")
                    .addClass("close")
                ;

                return result;

            }
        },
        "renderParam"      : {
            value: function () {
                var _this = this;

                var container = new mhjs.Div({"class": "param folder"});

                _this.levelChart = new LevelChart();

                var params = _this.renderTree("", {
                    "PCI" : _this.properties.param.PCI,
                    "RQI" : _this.properties.param.RQI,
                    "RDI" : _this.properties.param.RDI,
                    "SRI" : _this.properties.param.SRI,
                    "PSSI": _this.properties.param.PSSI
                });

                container
                    .append(_this.levelChart)
                    .append(params)
                ;

                return container;

            }
        },
        "renderYearCompare": {
            value: function () {
                var _this = this;

                var result = _this.renderTree("年度对比", {});

                result.label.on("click", function () {
                    result.toggleClass("close");
                });

                var table = new mhjs.Table({"class": "year"});
                var thead = new mhjs.Thead();
                var tbody = new mhjs.Tbody();

                table
                    .append(thead)
                    .append(tbody)
                ;

                var theadtr = new mhjs.Tr();

                thead
                    .append(theadtr)
                ;

                $.each([
                    "年份",
                    "PQI",
                    "PCI",
                    "RQI",
                    "RDI",
                    "SRI/PWI",
                    "PSSI"
                ], function (i, item) {
                    var th = new mhjs.Th();
                    th.text(item);
                    theadtr.append(th);
                });

                $.each([
                    "2020",
                    "2019",
                    "2018",
                    "2017",
                    "2016"
                ], function (i, item) {
                    var tr = new mhjs.Tr();

                    var td = new mhjs.Td();
                    td.text(item);
                    tr.append(td);

                    var td1 = new mhjs.Td();
                    tr.append(td1);

                    var td2 = new mhjs.Td();

                    tr.append(td2);

                    var td3 = new mhjs.Td();

                    tr.append(td3);

                    var td4 = new mhjs.Td();
                    tr.append(td4);

                    var td5 = new mhjs.Td();
                    tr.append(td5);

                    var td6 = new mhjs.Td();
                    tr.append(td6);

                    if (i === 0) {
                        td1.text(_this.properties.param.PQI);
                        td2.text(_this.properties.param.PCI);
                        td3.text(_this.properties.param.RQI);
                        td4.text(_this.properties.param.SRI);
                        td5.text(_this.properties.param.RDI);
                        td6.text(_this.properties.param.PSSI);
                    } else {

                        _this.renderRandomTd(_this.properties.param.PQI, td1);
                        _this.renderRandomTd(_this.properties.param.PCI, td2);
                        _this.renderRandomTd(_this.properties.param.RQI, td3);
                        _this.renderRandomTd(_this.properties.param.SRI, td4);
                        _this.renderRandomTd(_this.properties.param.RDI, td5);
                        _this.renderRandomTd(_this.properties.param.PSSI, td6);
                    }


                    tbody.append(tr);
                });

                result.child.append(table);

                result
                    .addClass("more")
                    .addClass("close")
                ;

                return result;

            }
        },
        "renderRandomTd"   : {
            value: function (value, td) {

                var rand = Math.random() * 2 - 1;
                var re = Math.min(value + rand, 100);

                td.text(re.toFixed(2));

                if ((re - value) > 0) {
                    td.addClass("up");
                } else if ((re - value) < 0) {
                    td.addClass("down");
                }
            }
        },
        "renderTree"       : {
            value: function (label, value) {

                var _this = this;
                var result;

                if (typeof value !== "object" || value === undefined) {
                    result = _this.renderItem(label, value);
                } else {
                    result = _this.renderFolder(label);

                    $.each(value, function (childLabel, childValue) {
                        result.child.append(_this.renderTree(childLabel, childValue));
                    });
                }

                return result;

            }
        },
        "renderFolder"     : {
            value: function (label) {

                var folder = new mhjs.Div({"class": "folder"});
                folder.label = new mhjs.Div({"class": "label"});
                folder.label.text(label);
                folder.child = new mhjs.Div({"class": "child"});

                folder
                    .append(folder.label)
                    .append(folder.child)
                ;
                return folder;
            }
        },
        "renderItem"       : {
            value: function (label, value) {

                var item = new mhjs.Div({"class": "item"});
                item.label = new mhjs.Div({"class": "label"});
                item.label.text(label);
                item.valuetext = new mhjs.Div({"class": "value"});
                item.valuetext.text(value);

                item
                    .append(item.label)
                    .append(item.valuetext)
                ;
                return item;
            }
        },
        "map"              : {
            set: function (map) {
                var _this = this;
                _this._map = new mhjs.Map(map);
            },
            get: function () {
                return this._map;
            }
        }
    });

    return _class;

});